package com.project.easy.common.framework.web.controller;

import com.project.easy.common.framework.captcha.core.CaptchaProcessor;
import com.project.easy.common.framework.captcha.core.vo.CaptchResultVo;
import com.project.easy.common.sequencer.SnowflakeIdWorker;
import  com.project.easy.common.util.constants.VersionConstant;
import  com.project.easy.common.util.dto.CaptchResultRes;
import  com.project.easy.common.util.dto.R;
import  com.project.easy.common.util.dto.TwoApiContextParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author GreatorMan.cool
 * @copyright: Copyright (c) 2020
 * @company: 扬起科技有限公司
 * @date 2021/11/24 22:05
 * @description:
 */
@ConditionalOnBean(CaptchaProcessor.class)
@RestController
@RequestMapping("/api/" + VersionConstant.ONE + "/zysoft/captcha")
public class CaptchaController extends BaseController {
    /**
     * 验证码
     */
    @Autowired(required = false)
    private CaptchaProcessor simpleCaptchaProcessor;
    /**
     * 返回信息
     * @param  oneApiContextParam 请求参数
     * @return 图片验证码
     */
    @PostMapping(value = "/getCaptcha")
    public R<Object> getCaptcha(@RequestBody TwoApiContextParam oneApiContextParam) {
        if (Objects.isNull(simpleCaptchaProcessor)) {
            return R.fail("系统没有配置");
        }
        CaptchResultVo captchResultVo = simpleCaptchaProcessor.create(SnowflakeIdWorker.nextIdStr(), oneApiContextParam.getOne(), oneApiContextParam.getTwo());
        CaptchResultRes captchResultRes = new CaptchResultRes(captchResultVo.getImageContent(), captchResultVo.getCaptchaKey());
        return R.success(captchResultRes);
    }
}
